package com.fyp.tests;

/*
 *  ============================================================================
 *  This file is part of 4YProject.
 *
 *  4YProject is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  4YProject is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with 4YProject; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *  ============================================================================
 */

/**
 *  ============================================================================
 *  The following class TestDBReturn.java provides an example that will return a
 *  servlet with information contained in the database.
 *
 *  @version 1.0
 *  @author  Sean Crowley
 *  @date    24 Jan 2008
 *  ============================================================================
 */

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestDBReturn extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		res.setContentType("text/html");
		PrintWriter out = res.getWriter();

		out.print("<html><head>");
		out.print("</head><body>");

		out.print("<form action=\"");
		out.print(req.getRequestURI());
		out.print("\" method=\"post\">");
		out.print("<input type=\"submit\" ");
		out.print("value=\" \">  ");
		out.print("Display Records</form>");

		out.print("</body></html>");

		out.close();
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		res.setContentType("text/html");
		PrintWriter out = res.getWriter();

		out.print("<html><head>");
		out.print("</head><body>");
//		out.print("<table style=text-align: left; width: 75%; border=1");
//		out.print("cellpadding=2 cellspacing=2>");
//		out.print("<tbody><tr><td>Hello</td><td></td></tr></tbody></table>");
		out.print("<code><pre>");

		// debugging info

		long time1 = System.currentTimeMillis();

		// connecting to database

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;

		try {
			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
			con = DriverManager.getConnection("jdbc:derby:/DerbyDB/AssetDB");

			stmt = con.createStatement();
			rs = stmt.executeQuery("SELECT * FROM Requests");

			// displaying records

			while (rs.next()) {
				out.print("<font color=red><b>Query</b></font> " + rs.getObject(1).toString() + "\t\n");
				out.print("<font color=green<b>Computer Name</b>\t\n</font>");
				out.print(rs.getObject(2).toString() + "\t\n");
				out.print("<font color=green><b>Title</b>\t\n</font>");
				out.print(rs.getObject(3).toString() + "\t\n");
				out.print("<font color=green><b>Description\t\n</b></font>");
				out.print(rs.getObject(4).toString() + "\n\n");
			}

		} catch (SQLException e) {
			throw new ServletException("Servlet Could not display records.", e);
		} catch (ClassNotFoundException e) {
			throw new ServletException("JDBC Driver not found.", e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (SQLException e) {
			}
		}

		// debugging info

		long time2 = System.currentTimeMillis();

		out.print("</pre></code>");

		out.print("<p>Search took : ");
		out.print((time2 - time1));
		out.print(" ms.</p>");

		out.print("<p\"><a href=\"");
		out.print(req.getRequestURI());
		out.print("\">Back</a></p>");

		out.print("</body></html>");
		out.close();
	}
}
